﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CustomTypes.Interfaces;
using devtm.AutoMapper;
using System.Diagnostics;
using System.Collections;
using Microsoft.RestrictedUsage.CSharp.Compiler;
using Microsoft.RestrictedUsage.CSharp.Semantics;

namespace CustomTypes.Mono.Resolvers
{

    public class AssemblyCSharpDescriptorResolver : CustomTypes.Interfaces.IAssemblyDescriptorResolver
    {


        private TypeRepository typeRepository;
        public AssemblyCSharpDescriptorResolver(TypeRepository typeRepository)
        {
            this.typeRepository = typeRepository;            
        }


        public IAssemblyDescriptor FindAssembly(Dictionary<string, IAssemblyDescriptor> container, string assemblyName, object obj)
        {

            var prj = ContainingProject;

            foreach (EnvDTE.Project item in prj.GetReferencedProjectListe(false))
            {
                var prj2 = new devtm.Editor.TypeSelector.ParsingSolution.Project(item);
                if (!container.ContainsKey(prj2.AssemblyName))
                {

                    Compilation compilation = CSharpTextSelection.LanguageModel.CreateCompiler(item).GetCompilation();
                    CustomTypes.Mono.AssemblyTypeCSharpDescriptor t = new AssemblyTypeCSharpDescriptor(typeRepository, compilation.MainAssembly);
                    container.Add(prj2.AssemblyName, t);
                    
                    if (assemblyName == prj2.AssemblyName)
                        return t;

                }
            }


            return null;

        }

        public Hashtable Paths { get; set; }

        public devtm.Editor.TypeSelector.ParsingSolution.Project ContainingProject { get; set; }

    }
}
